package greedy;

/**
 * Created by lh on 2019/7/23.
 * 题目描述：
 * flowerbed 数组中 1 表示已经种下了花朵。花朵之间至少需要一个单位的间隔，求解是否能种下 n 朵花
 *
 * Example:
 * Input: flowerbed = [1,0,0,0,1], n = 1
 * Output: True
 */
public class PlaceFlowers {
    public static boolean canPlaceFlowers(int[] flowerbed,int n) {
        int length = flowerbed.length;
        int sum = 0;
        for (int i = 0; i < length && sum < n; i++) {
            if (flowerbed[i] == 1) {
                continue;
            }
            int left = i == 0 ? 0 : flowerbed[i - 1];
            int right = i == length - 1 ? 0 : flowerbed[i + 1];
            if (left == 0 && right == 0) {
                sum++;
                flowerbed[i] = 1;
            }
        }
        return sum >= n;
    }
}
